package cn.JHai.controller;

import cn.JHai.domain.User;
import cn.JHai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        System.out.println("表现层，查询所有用户。。。");
        List<User> userList = userService.findAll();

        model.addAttribute("list", userList);
        for (User user:userList){
            System.out.println(user);
        }

        return "index";
    }

    @RequestMapping("/save")
    public String save(MultipartFile fileImg, User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = request.getSession().getServletContext().getRealPath("/img/");
        // 判断，该路径是否存在
        File file = new File(path);
        if(!file.exists()){
            // 创建该文件夹
            file.mkdirs();
        }

        //System.out.println(fileImg);
        if (fileImg!=null){
            String filename = fileImg.getOriginalFilename();
            // 把文件的名称设置唯一值，uuid
            String uuid = UUID.randomUUID().toString().replace("-", "");
            filename = uuid+"_"+filename;
            // 完成文件上传
            fileImg.transferTo(new File(path,filename));
            user.setHeadImage(path+filename);
        }
        else{
            user.setHeadImage(path+"个人中心.png");
        }

        userService.insert(user);
        return "registe";


    }

    @RequestMapping("/updata")
    public String update(int id,MultipartFile fileImg, User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = request.getSession().getServletContext().getRealPath("/img/");
        // 判断，该路径是否存在
        File file = new File(path);
        if(!file.exists()){
            // 创建该文件夹
            file.mkdirs();
        }

        //System.out.println(fileImg);
        if (fileImg!=null){
            String filename = fileImg.getOriginalFilename();
            // 把文件的名称设置唯一值，uuid
            String uuid = UUID.randomUUID().toString().replace("-", "");
            filename = uuid+"_"+filename;
            // 完成文件上传
            fileImg.transferTo(new File(path,filename));
            user.setHeadImage(path+filename);
        }
        else{
            user.setHeadImage(path+"个人中心.png");
        }
        user.setId(id);
        userService.update(user);
        return "registe";


    }

    //登陆
    @RequestMapping("/login")
    public void register(int uid,HttpServletResponse response,String name,String password) throws IOException {
        PrintWriter out =response.getWriter();
        if (userService.find(uid)==null){
            out.write("false");
        }else{
            out.write("true");
        }

    }

    //注销
    @RequestMapping("/invalidata")
    public String invalidata(int uid,HttpServletRequest request,String name,String password) throws IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        return  "index";

    }



}
